home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 248_01 / check.c < prev    next >
Text File  |  1989-08-16  |  5KB  |  212 lines

  1. /*    CHECK:    Dictionary scan module for MicroSPELL 1.0
  2.         Spell Checker and Corrector
  3.  
  4.         (C)opyright May 1987 by Daniel Lawrence
  5.         All Rights Reserved
  6.  
  7. */
  8.  
  9. #include    <stdio.h>
  10. #include    "dopt.h"
  11. #include    "dstruct.h"
  12. #include    "ddef.h"
  13.  
  14. check()
  15.  
  16. {
  17.     int i;        /* index into the word list */
  18.  
  19.     /* sort the source words */
  20.     if (swdebug)
  21.         printf("[%u words being checked...\n", numwords);
  22.     wordsort();
  23.     if (swdebug)
  24.         printf("  sorted...");
  25.  
  26.     /* merge them against the uppercase part of the dictionary */
  27.     umerge();
  28.     if (swdebug)
  29.         printf("Uppercase checked\n");
  30.  
  31.  
  32.     /* lowercase the source word list */
  33.     wordlow();
  34.  
  35.     /* resort them */
  36.     wordsort();
  37.     if (swdebug)
  38.         printf("  sorted...");
  39.  
  40.     /* merge them against the rest the dictionary */
  41.     merge();
  42.     if (swdebug)
  43.         printf("lowercase checked\n");
  44.  
  45.     /* sort them back into position order */
  46.     possort();
  47.     if (swdebug) {
  48.         printf("  sorted in position order...");
  49.         printf("%u mismatched words]\n", badwords);
  50.     }
  51.  
  52.     /* dump the list to disk */
  53.     dumplist();
  54.  
  55.     /* lastly, de-allocate the word list */
  56.     while (numwords > 0)
  57.         free(sword[--numwords]);
  58. }
  59.  
  60. int merge()        /* do a merge run against the main dictionary    */
  61.  
  62. {
  63.     register int cindex;    /* current word index */
  64.     register char *cword;    /* ptr to current word */
  65.     register int cmp;    /* result of current comparision */
  66.     register int mismatch;    /* number of mismatched words */
  67.     char mword[NSTRING];    /* current dictionary word */
  68.  
  69.     /* start with a LOW VALUES dictionary word */
  70.     mword[0] = 0;
  71.     mismatch = 0;
  72.  
  73.     strcpy(mword, nxtmword());
  74.  
  75.     for (cindex = 0; cindex < numwords; cindex++) {
  76.  
  77.         /* get the current word */
  78.         cword = sword[cindex]->w_text;
  79.  
  80.         /* scan the dictionary for a match */
  81.         cmp = strcmp(cword, mword);
  82.         while (cmp > 0) {
  83.             strcpy(mword, nxtmword());
  84.             cmp = strcmp(cword, mword);
  85.         }
  86.  
  87.         /* if this word is not matched..... */
  88.         if (cmp != 0) {
  89.             if (mismatch == cindex)
  90.                 ++mismatch;
  91.             else {
  92.                 sword[mismatch++] = sword[cindex];
  93.                 sword[cindex] = NULL;
  94.             }
  95.         } else {
  96.             free(sword[cindex]);
  97.             sword[cindex] = NULL;
  98.         }
  99.     }
  100.     numwords = mismatch;
  101.     badwords += mismatch;
  102.     mclose();
  103. }
  104.  
  105. /* do a merge run against the upper case entries in the main dictionary    */
  106.  
  107. int umerge()
  108.  
  109. {
  110.     register int cindex;    /* current word index */
  111.     register char *cword;    /* ptr to current word */
  112.     register int cmp;    /* result of current comparision */
  113.     register int mismatch;    /* number of mismatched words */
  114.     char mword[NSTRING];    /* current dictionary word */
  115.  
  116.     if (mopen() == FALSE)
  117.         exit(EXMDICT);
  118.  
  119.     mword[0] = 0;        /* start with a LOW VALUES dictionary word */
  120.     mismatch = 0;
  121.  
  122.     strcpy(mword, nxtmword());
  123.  
  124.     for (cindex = 0; cindex < numwords; cindex++) {
  125.  
  126.         /* get the current word */
  127.         cword = sword[cindex]->w_text;
  128.  
  129.         /* scan the dictionary for a match */
  130.         cmp = strcmp(cword, mword);
  131.         while (cmp > 0 && mword[0] != SEPCHAR) {
  132.             strcpy(mword, nxtmword());
  133.             cmp = strcmp(cword, mword);
  134.         }
  135.  
  136.         /* if we reach the end of the capitals...stop */
  137.         if (mword[0] == SEPCHAR)
  138.             break;
  139.  
  140.         /* if this word is not matched..... */
  141.         if (cmp != 0) {
  142.             if (mismatch == cindex)
  143.                 ++mismatch;
  144.             else {
  145.                 sword[mismatch++] = sword[cindex];
  146.                 sword[cindex] = NULL;
  147.             }
  148.         } else {
  149.             free(sword[cindex]);
  150.             sword[cindex] = NULL;
  151.         }
  152.     }
  153.  
  154.     /* move the rest of the source words down in the list */
  155.     if (cindex < numwords && mismatch != cindex) {
  156.         for (; cindex < numwords; cindex++) {
  157.             sword[mismatch++] = sword[cindex];
  158.             sword[cindex] = NULL;
  159.         }
  160.         numwords = mismatch;
  161.     }
  162.  
  163. }
  164.  
  165. wordlow()    /* lowercase all the words */
  166.  
  167. {
  168.     register cindex;        /* index into the word list */
  169.     register char *ptr;        /* ptr to current word */
  170.  
  171.     for (cindex = 0; cindex < numwords; cindex++) {
  172.         ptr = sword[cindex]->w_text;
  173.         while (*ptr) {
  174.             if (isupper(*ptr))
  175.                 *ptr += ('a' - 'A');
  176.             ++ptr;
  177.         }
  178.     }
  179. }
  180.  
  181. dumplist()
  182.  
  183. {
  184.     int i;
  185.  
  186.     /* if there is a user dictionary, dump its name out */
  187.     if (*userlist)
  188.         if (swwords)
  189.             fprintf(outfile, "USER LIST: %s\n", userlist);
  190.         else
  191.             fprintf(outfile, "-3\n%s\n", userlist);
  192.  
  193.     for (i = 0; i < numwords; i++) {
  194.  
  195.         /* if we are in a new file... write a file header */
  196.         if (outnum != sword[i]->w_file) {
  197.             outnum = sword[i]->w_file;
  198.             if (swwords)
  199.                 fprintf(outfile, "FILE: %s\n", splname[outnum]);
  200.             else
  201.                 fprintf(outfile, "-1\n%s\n", splname[outnum]);
  202.         }
  203.  
  204.         /* write the row and column number of the current word */
  205.         if (swwords)
  206.             fprintf(outfile, "%s\n", sword[i]->w_text);
  207.         else
  208.             fprintf(outfile, "%u\n%u\n",
  209.                 sword[i]->w_line, sword[i]->w_col);
  210.     }
  211. }
  212.